/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.base.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.modules.base.dao.StoreCardTemplateProductDao;
import com.chlitina.store.modules.base.entity.StoreCardTemplateAll;
import com.chlitina.store.modules.base.entity.StoreCardTemplateProduct;
import com.chlitina.store.modules.sys.entity.Store;
import com.chlitina.store.modules.sys.utils.StoreUtils;

/**
 * 产品充值卡模板Service
 * @author liujiang
 * @version 2013-11-03
 */
@Component
@Transactional(readOnly = true)
public class StoreCardTemplateProductService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(StoreCardTemplateProductService.class);
	
	@Autowired
	private StoreCardTemplateProductDao storeCardTemplateProductDao;
	
	public StoreCardTemplateProduct get(Long id) {
		return storeCardTemplateProductDao.findOne(id);
	}
	
	public List<StoreCardTemplateProduct> getByStore() {
		DetachedCriteria dc = storeCardTemplateProductDao.createDetachedCriteria();
		dc.add(Restrictions.eq("store", StoreUtils.getStore()));
		dc.addOrder(Order.asc("id"));
		return storeCardTemplateProductDao.find(dc);
	}
	
	public Page<StoreCardTemplateProduct> find(Page<StoreCardTemplateProduct> page, 
		StoreCardTemplateProduct storeCardTemplateProduct, String searchStatus) {
		Store store = StoreUtils.getStore();
		DetachedCriteria dc = storeCardTemplateProductDao.createDetachedCriteria();
		if (searchStatus == null || "0".equals(searchStatus)) {
			dc.add(Restrictions.eq("status", StoreCardTemplateAll.FLAG_ENABLE));
		} else if ("1".equals(searchStatus)) {
			dc.add(Restrictions.like("status", StoreCardTemplateAll.FLAG_DISABLE));
		}
		dc.add(Restrictions.eq("store", store));
		dc.addOrder(Order.desc("id"));
		return storeCardTemplateProductDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(StoreCardTemplateProduct storeCardTemplateProduct) {
		storeCardTemplateProductDao.save(storeCardTemplateProduct);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		storeCardTemplateProductDao.changeStatus(StoreCardTemplateProduct.FLAG_DISABLE, id);
	}
	
}
